import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { HttpSeriveProvider } from "../http-serive/http-serive";
import { Observable } from "rxjs/Observable";
// import * as b from "jsonp";
import { MySeriveProvider } from "../my-serive/my-serive";
import { Header } from "ionic-angular/components/toolbar/toolbar-header";
/*
  Generated class for the TeamSeriveProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class TeamSeriveProvider {
  public headers = new HttpHeaders();
  token;
  constructor(
    public http: HttpClient,
    public httpSeriveProvider: HttpSeriveProvider,
    public mySeriveProvider: MySeriveProvider
  ) {
    //请求头的设置
    this.token = localStorage.getItem("token");

    this.headers = this.headers.set("Content-Type", "text/plain");
    this.headers = this.headers.set("Content-Type", "application/json");
    if (this.token) {
      this.headers = this.headers.set("token", this.token);
    } else if (this.token === null || this.token === undefined) {
      this.headers = this.headers.set("token", "");
    } else {
      this.headers = this.headers.set("token", null);
    }
  }

  setToken() {
    this.headers = this.headers.set("token", localStorage.getItem("token"));
  }

  /**
   * base64图片上传接口
   * @param base 图片的base格式
   */
  upload(base: string): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}photo/uploadbase64?uploadPhoto=${base}`,
      // JSON.stringify({ uploadPhoto: base })
      {},
      { headers: this.headers }
    );
  }

  /**
   *获取团队信息
   * @param teamCode 团队ID
   */
  getTeamDetails(teamCode: number): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/Desc/${teamCode}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   * 获取加入的团队
   */
  getJoinTeam(): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/jion/teams?param=${new Date()}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   *加入团队
   * @param teamcode 团队编号
   */
  joinTeam(teamcode: number): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}user/jion/team`,
      { teamcode },
      { headers: this.headers }
    );
  }

  /**
   *创建团队
   * @param teamName 团队名称
   * @param teamicon 团队头像 (需要先经过上传接口，再获取上传接口返回的字符串)
   */
  cerateTeam(teamname: string, teamicon: string) {
    let stringTeam = JSON.stringify({
      teamname,
      teamicon
    });
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/create`,
      stringTeam,
      { headers: this.headers }
    );
  }

  /**
   *判断团队名称是否重复
   * @param teamname 团队名称
   */
  getTeamNameRepeat(teamname: string): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/check/name`,
      { teamname },
      { headers: this.headers }
    );
  }

  /**
   * 获得当前用户创建的团队列表
   */
  getMyCreateTeams(): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/create/all?param=${new Date()}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   * 获取所有团队列表
   */
  getTeamAll(): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/all?param=${new Date()}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   * 是否有权限创建团队
   */
  getCanCreate(): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/check?param=${new Date()}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   * 是否有权限创建评测
   */
  getCanCreateEvaluating(teamCode) {
    let h = this.headers;
    h.delete("Content-Type");
    h = h.set("Content-Type", "application/json");
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}evaluate/check`,
      { teamcode: teamCode },
      { headers: h }
    );
  }

  /**
   * 判断团队是否加入
   * @param teamcode 团队code
   */
  getIsJoinTeam(teamcode) {
    let h = this.headers;
    h.delete("Content-Type");
    h = h.set("Content-Type", "application/json");
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/jion/is`,
      { teamcode },
      { headers: h }
    );
  }

  /**
   * 退出团队
   * @param teamcode 团队code
   */
  exitTeam(teamcode): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/exit`,
      { teamcode },
      { headers: this.headers }
    );
  }

  /**
   * 获取个人信息接口
   * @param userCode 用户code
   */
  getUserInfo(userCode) {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}user/info/userCode/${userCode}`,
      {},
      { headers: this.headers }
    );
  }

  /**
   * 踢出团队成员
   * @param teamcode 团队编号
   * @param usercode 你要踢的人的编号
   */
  removeMember(teamcode, usercode): Observable<any> {
    return this.httpSeriveProvider.post(
      `${this.httpSeriveProvider.api}team/winkle`,
      {
          teamcode, 
          usercode
      },
      { headers: this.headers }
    );
  }
}
